package henu.interceptor;


import henu.bean.Admin;
import henu.bean.ManagePerson;
import henu.bean.PInformation;
import henu.util.CONSTANTS;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import oracle.net.aso.a;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class PermissionInterceptor implements Interceptor {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void destroy() {

	}

	@Override
	public void init() {

	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		/*获取当前action以及namespace*/
		ActionContext act = invocation.getInvocationContext();		//struts的上下文
		HttpSession session =  ServletActionContext.getRequest().getSession();
		String namespace = invocation.getProxy().getNamespace() ; 
		//StringBuilder builder = new StringBuilder(namespace);
		//String currentaction = builder.append("/").append(act.getName()).toString(); //当前action的名字
		//System.out.println("当前的action："+currentaction);
		/*获取允许访问的链接*/
		List<String> allowlist =  (List<String>) act.getSession().get(CONSTANTS.ALLOWLIST);
		if(allowlist == null)
		{
			allowlist = new ArrayList<String>();
			//
		}
		/*	公共action 放行	*/
		//allowlist.add("/admin/adminAction_login");	
		if(allowlist.contains(namespace))
		{
			return invocation.invoke();
		}
		/*权限不许可，直接跳转到权限不足的界面*/
		return "nopermission";
	}
}
